package com.Monica.servlet;

import com.Monica.until.CookieUtils;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CookieServlet extends BaseServlet{
    protected void testPath(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie = new Cookie("path1","path1");
        cookie.setPath(req.getContextPath() + "/abc");
        resp.addCookie(cookie);
        resp.getWriter().write("创建了一个带有Path路径的Cookie");
    }
    protected void life3600(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie = new Cookie("life3600","life3600");
        cookie.setMaxAge(60 * 60);
        resp.addCookie(cookie);
        resp.getWriter().write("已经创建了一个存活1小时的Cookie");
    }
    protected void deleteNow(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie = CookieUtils.findCookie("key4",req.getCookies());
        //调用setMaxAge(0)
        if (cookie != null){
        //先找到你要删除的Cookie对象
            cookie.setMaxAge(0);
            //调用response.addCookie(cookie)
            resp.addCookie(cookie);

            resp.getWriter().write("key4已经被删除");
        }



    }
    protected void defaultLife(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie = new Cookie("defalutLife","defalutLife");
        cookie.setMaxAge(-1);//设置存活时间  默认情况下浏览器一关  cookie就没有了
        resp.addCookie(cookie);
    }
    protected void updateCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //方案一：
//        Cookie cookie = new Cookie("key1","newValue1");
//        resp.addCookie(cookie);
//        resp.getWriter().write("key1的Cookie已经修改好");
        //方案二：
        Cookie cookie = CookieUtils.findCookie("key2",req.getCookies());
        if (cookie != null){
            cookie.setValue("newValue2");
            resp.addCookie(cookie);
        }

    }
    protected void getCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie[] cookies = req.getCookies();
        for (Cookie cookie : cookies) {
            //getName方法返回Cookie的key（名）
            //getValue方法返回Cookie的value值
            resp.getWriter().write("Cookie[" + cookie.getName() + "=" + cookie.getValue() + "] <br/>");
        }

        Cookie iwantCookie = CookieUtils.findCookie("key2",cookies);
//        for (Cookie cookie : cookies) {
//            if("key2".equals(cookie.getName())){
//                iwantCookie = cookie;
//                break;
//            }
//        }
        if (iwantCookie != null){
            resp.getWriter().write("找到了需要的cookie");
        }
    }
    protected void createCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.创建Cookie对象
        Cookie cookie3 = new Cookie("key1","value1");
        resp.addCookie(cookie3);
        Cookie cookie = new Cookie("key2","value2");
        resp.addCookie(cookie);
        Cookie cookie1 = new Cookie("key3","value3");
        resp.addCookie(cookie1);
        Cookie cookie4 = new Cookie("key4","value4");
        resp.addCookie(cookie4);
        Cookie cookie5 = new Cookie("key5","value5");
        resp.addCookie(cookie5);

        resp.getWriter().write("Cookie创建成功");
    }
}
